package com.hakim.wfb.customer.dto;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

public class WeixinAccessToken {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 获取到的凭证
	 */
	private String accessToken;
	/**
	 * 凭证有效时间，单位：秒
	 */
	private Integer expiresIn;
	
	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public Integer getExpiresIn() {
		return expiresIn;
	}

	public void setExpiresIn(Integer expiresIn) {
		this.expiresIn = expiresIn;
		this.expiredTimeMillis = System.currentTimeMillis()+expiresIn*1000;
	}

	/**
	 * 过期时间的毫秒数
	 */
	private long expiredTimeMillis;
	
	/**
	 * 判断access_token是否过期
	 */
	public boolean isExpired() {
		return expiredTimeMillis < System.currentTimeMillis();
	}
	/**
	 * 判断是否可用
	 */
	public boolean isValid() {
		return !isExpired() && accessToken != null;
	}


	public String toStringInfo() {
		return JSON.toJSONString(this);
	}

	public static WeixinAccessToken fromStringInfo(String string) {
		if (StringUtils.isNotBlank(string)) {
			return JSON.parseObject(string, WeixinAccessToken.class);
		}
		return null;
	}
}
